/*
*
*常用工具函数
*
*/

'use strict';
const UA = window.navigator.userAgent.toLowerCase()
const isFF = UA && UA.match(/firefox\/(\d+)/)
import store from "@/store";
export default {
    /**
     * 每个插件都有的install方法，用于安装插件
     * @param {Object} Vue - Vue类
     * @param {Object} [pluginOptions] - 插件安装配置
     */
    install: function (Vue, option) {
        //检测数据的类型
        //使用方法：this.$dataTypes.isNull(null);===> true
        //
        Vue.prototype.$scale = function(){
            return (isFF?screen.width:document.documentElement.clientWidth)/store.state.setting.screenWidth;
        }
        Vue.prototype.$download = function(res,name){
            const a = document.createElement('a')
            a.href = URL.createObjectURL(res)
            a.download = name  // 下载文件的名字
            a.click()
        }

        Vue.prototype.$addClass = function  (el, cls) {
            /* istanbul ignore if */
            if (!cls || !(cls = cls.trim())) {
                return
            }

            /* istanbul ignore else */
            if (el.classList) {
                if (cls.indexOf(' ') > -1) {
                    cls.split(whitespaceRE).forEach(c => el.classList.add(c))
                } else {
                    el.classList.add(cls)
                }
            } else {
                const cur = ` ${el.getAttribute('class') || ''} `
                if (cur.indexOf(' ' + cls + ' ') < 0) {
                    el.setAttribute('class', (cur + cls).trim())
                }
            }
        }
        Vue.prototype.$removeClass =  function(el, cls){
            /* istanbul ignore if */
            if (!cls || !(cls = cls.trim())) {
                return
            }
            /* istanbul ignore else */
            if (el.classList) {
                if (cls.indexOf(' ') > -1) {
                    cls.split(whitespaceRE).forEach(c => el.classList.remove(c))
                } else {
                    el.classList.remove(cls)
                }
                if (!el.classList.length) {
                    el.removeAttribute('class')
                }
            } else {
                let cur = ` ${el.getAttribute('class') || ''} `
                const tar = ' ' + cls + ' '
                while (cur.indexOf(tar) >= 0) {
                    cur = cur.replace(tar, ' ')
                }
                cur = cur.trim()
                if (cur) {
                    el.setAttribute('class', cur)
                } else {
                    el.removeAttribute('class')
                }
            }
        }
        Vue.prototype.$isFF = isFF
        Vue.prototype.$getDocumentHeight = function(){
            return document.documentElement.clientHeight/this.$scale();
        }
        Vue.prototype.$getQueryString = function(name){
            const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            const r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return unescape(r[2]);
            }
            return null;
        }
        Vue.prototype.$dataTypes = {
            isPrototype(data) {
                return Object.prototype.toString.call(data).toLowerCase()
            },
            isArray(data) {
                return this.isPrototype(data) === '[object array]'
            },
            isJSON(data) {
                return this.isPrototype(data) === '[object object]'
            },
            isFunction(data) {
                return this.isPrototype(data) === '[object function]'
            },
            isString(data) {
                return this.isPrototype(data) === '[object string]'
            },
            isNumber(data) {
                return this.isPrototype(data) === '[object number]'
            },
            isUndefined(data) {
                return this.isPrototype(data) === '[object undefined]'
            },
            isNull(data) {
                return this.isPrototype(data) === '[object null]'
            }
        }
        //添加时间戳转换
        Vue.prototype.$formatTime = function (time) {
            let date = new Date(time);
            let y = date.getFullYear();
            let m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            let d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            let h = date.getHours();
            h = h < 10 ? ('0' + h) : h;
            let minute = date.getMinutes();
            let second = date.getSeconds();
            minute = minute < 10 ? ('0' + minute) : minute;
            second = second < 10 ? ('0' + second) : second;
            return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
        }
        //添加时间戳转换
        Vue.prototype.$formatTimeYYYYMMDD = function (date) {
            let y = date.getFullYear();
            let m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            let d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d + ' '
        }
        /**
         *
         * 日期比较大小
         * date1/date2:日期格式  非时间戳
         * 返回 date1是否大于date2
         */
        Vue.prototype.$compareDate = function (date1, date2) {
            if (!date1 && !date2) {
                return;
            }

            let d1 = (date1.indexOf('-') > -1) && date1.replace(/-/g, '/');
            let d2 = (date2.indexOf('-') > -1) && date2.replace(/-/g, '/');
            return new Date(d1).getTime() - new Date(d2).getTime();
        }

        // 获取当前日期
        Vue.prototype.$getTodayDate = function () {
            let date = new Date(),
                year = date.getFullYear(), //获取完整的年份(4位)
                month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
                strDate = date.getDate() // 获取当前日(1-31)
            if (month >= 1 && month <= 9) month = '0' + month // 如果月份是个位数，在前面补0
            if (strDate >= 0 && strDate <= 9) strDate = '0' + strDate // 如果日是个位数，在前面补0

            let currentdate = year + '-' + month + '-' + strDate
            return currentdate
        }
        // 获取当前日期时分秒
        Vue.prototype.$getTodayHour = function () {
            let date = new Date(),
                year = date.getFullYear(), //获取完整的年份(4位)
                month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
                strDate = date.getDate(), // 获取当前日(1-31)
                hour = date.getHours(),//时
                minute = date.getMinutes(),//分
                second = date.getSeconds()//秒
            if (month >= 1 && month <= 9) month = '0' + month // 如果月份是个位数，在前面补0
            if (strDate >= 0 && strDate <= 9) strDate = '0' + strDate // 如果日是个位数，在前面补0
            if (hour >= 0 && hour <= 9) hour = '0' + hour
            if (minute >= 0 && minute <= 9) minute = '0' + minute
            if (second >= 0 && second <= 9) second = '0' + second
            let currentdate = year + '-' + month + '-' + strDate + ' ' + hour + ':' + minute + ':' + second
            return currentdate
        }
        // 获取昨天日期
        Vue.prototype.$getYesterdayDate = function (time) {
            let date = new Date(time);
            let newtimems= date.getTime()-(24*60*60*1000);
            let yesd = new Date(newtimems);
            let yesYear = yesd.getFullYear();
            let yesMonth = yesd.getMonth() + 1;
            let yesDate = yesd.getDate();
            if (yesMonth >= 1 && yesMonth <= 9) yesMonth = '0' + yesMonth // 如果月份是个位数，在前面补0
            if (yesDate >= 0 && yesDate <= 9) yesDate = '0' + yesDate // 如果日是个位数，在前面补0
            return yesYear+"-"+yesMonth+"-"+yesDate;
        }
        // 获取明天日期
        Vue.prototype.$getTomorrowDate = function (time) {
            let date = new Date(time);
            let newtimems= date.getTime()+(24*60*60*1000);
            let tomd = new Date(newtimems);
            let tomYear = tomd.getFullYear();
            let tomMonth = tomd.getMonth() + 1;
            let tomDate = tomd.getDate();
            if (tomMonth >= 1 && tomMonth <= 9) tomMonth = '0' + tomMonth // 如果月份是个位数，在前面补0
            if (tomDate >= 0 && tomDate <= 9) tomDate = '0' + tomDate // 如果日是个位数，在前面补0
            return tomYear+"-"+tomMonth+"-"+tomDate;
        }
        // 获取一个月后日期
        Vue.prototype.$getNextMonthDate = function (time) {
            let date = new Date(time);
            let newtimems= date.getTime()+(30*24*60*60*1000);
            let tomd = new Date(newtimems);
            let tomYear = tomd.getFullYear();
            let tomMonth = tomd.getMonth() + 1;
            let tomDate = tomd.getDate();
            if (tomMonth >= 1 && tomMonth <= 9) tomMonth = '0' + tomMonth // 如果月份是个位数，在前面补0
            if (tomDate >= 0 && tomDate <= 9) tomDate = '0' + tomDate // 如果日是个位数，在前面补0
            return tomYear+"-"+tomMonth+"-"+tomDate;
        }
        // 获取上一个月日期
        Vue.prototype.$getLastMonthDate = function (time) {
            let date = new Date(time);
            let newtimems= date.getTime()-(30*24*60*60*1000);
            let tomd = new Date(newtimems);
            let tomYear = tomd.getFullYear();
            let tomMonth = tomd.getMonth() + 1;
            let tomDate = tomd.getDate();
            if (tomMonth >= 1 && tomMonth <= 9) tomMonth = '0' + tomMonth // 如果月份是个位数，在前面补0
            if (tomDate >= 0 && tomDate <= 9) tomDate = '0' + tomDate // 如果日是个位数，在前面补0
            return tomYear+"-"+tomMonth+"-"+tomDate;
        }
    }
}
